﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.GERP.GHRMS.MODEL;
using System.Configuration;
using System.Data;
using KPIS.DBM;

namespace KPIS.GERP.GHRMS.DAL
{
    public class TR_TrainerDistrictDAL
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<TR_TrainerDistrictInfo> GetFill()
        {   //เช็คว่า Data Table มีข้อมูลหรือไม่
            DataRowCollection drc = null;
            IList<TR_TrainerDistrictInfo> infoList = new List<TR_TrainerDistrictInfo>();
            try
            {
                dbManager.Open();

                string strSQL = "SELECT mas_districts.district_seq, mas_districts.district_name"
                                + " ,mas_amphurs.amp_name, mas_provinces.prov_name"
                                + " FROM mas_amphurs"
                                + " Inner Join mas_provinces ON mas_provinces.prov_seq = mas_amphurs.prov_seq"
                                + " Inner Join mas_districts ON mas_amphurs.amp_seq = mas_districts.amp_seq"
                                + " WHERE mas_districts.record_status not like 'D'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;

                if (drc.Count > 0)
                {
                    int i = 0;

                    while (i < drc.Count)
                    {
                        TR_TrainerDistrictInfo info = new TR_TrainerDistrictInfo();
                        info.district_seq = Convert.ToInt32(drc[i]["district_seq"]);
                        string dtrProvName = drc[i]["prov_name"].ToString();
                        if (dtrProvName == "กรุงเทพมหานคร")
                        {                            
                            info.district_name = "แขวง" + drc[i]["district_name"].ToString();
                            info.amp_name = drc[i]["amp_name"].ToString();
                            info.prov_name = "จังหวัด" + drc[i]["prov_name"].ToString();
                        }
                        else
                        {                          
                            info.district_name = "ตำบล" + drc[i]["district_name"].ToString();
                            info.amp_name = "อำเภอ" + drc[i]["amp_name"].ToString();
                            info.prov_name = "จังหวัด" + drc[i]["prov_name"].ToString();                            
                        }
                        info.district = info.district_name +""+ info.amp_name +""+ info.prov_name;
                        infoList.Add(info);
                        i++;
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose(); //สั้งเคลียร์ Connections ของ Method
            }
            return infoList;
        }
    }
}
